﻿using System;

namespace Freedom.Common.Collections
{
    /// <summary>
    /// CollectionChanged Event Handler
    /// </summary>
    public delegate void CollectionChangedEventHandler<T>(object sender, CollectionChangedEventArgs<T> e);

    /// <summary>
    /// CollectionChangedAction
    /// </summary>
    public enum CollectionChangedAction
    {
        /// <summary>
        /// Items added
        /// </summary>
        Add,
        /// <summary>
        /// Items updated
        /// </summary>
        Update,
        /// <summary>
        /// Items removed
        /// </summary>
        Remove,
        /// <summary>
        /// All collection items reloaded
        /// </summary>
        Reset,

        /// <summary>
        /// When response to the event from service, insert the new record to the first
        /// </summary>
        InsertToTop,
    }

    /// <summary>
    /// CollectionChangedEventArgs
    /// </summary>
    public class CollectionChangedEventArgs<T> : EventArgs
    {
        /// <summary>
        /// Gets or sets the action.
        /// </summary>
        /// <value>The action.</value>
        public CollectionChangedAction Action { get; private set; }

        /// <summary>
        /// Gets or sets the objects.
        /// </summary>
        /// <value>The objects.</value>
        public T[] Items { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionChangedEventArgs&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="items">The items.</param>
        public CollectionChangedEventArgs(CollectionChangedAction action, T[] items)
        {
            Items = items;
            Action = action;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionChangedEventArgs&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="item">The item.</param>
        public CollectionChangedEventArgs(CollectionChangedAction action, T item)
        {
            Items = new[] { item };
            Action = action;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionChangedEventArgs&lt;T&gt;"/> class.
        /// </summary>
        public CollectionChangedEventArgs()
        {
            Items = null;
            Action = CollectionChangedAction.Reset;
        }
    }
}
